package cn.itcast.nems.order.web.controller.o;

import cn.itcast.nems.common.util.LocalDateTimeUtils;
import cn.itcast.nems.journal.service.JournalDiscountService;
import cn.itcast.nems.order.dto.JournalDiscountApiReqDTO;
import cn.itcast.nems.order.dto.JournalDiscountApiResDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 优惠凭单相关逻辑
 *
 * @author ChangLu.Gao
 * @version v1.0
 * @program nems-server
 * @description
 * @date 2023/12/08 10:56
 */
@RestController("ojournalDiscountController")
@RequestMapping("/o/journal/discount")
@Slf4j
@RequiredArgsConstructor
public class JournalDiscountController {

    private final JournalDiscountService journalDiscountService;

    /**
     * 根据条件获取可用的优惠单
     *
     * @param queryVO 请求参数
     * @return org.springframework.http.ResponseEntity<cn.itcast.nems.order.dto.JournalDiscountDTO>
     * @author 高昌露
     * @since 2023/12/10 16:04
     */
    @GetMapping("/valid")
    public ResponseEntity<List<JournalDiscountApiResDTO>> usableList(
            @RequestParam(name = "customer_id", required = false) String customerId,
            @RequestParam(name = "student_id", required = false) Integer studentId,
            @RequestParam(name = "target_clazz_id") Integer targetClazzId,
            @RequestParam(name = "biz_date_time", required = false) String bizDateTime,
            @RequestParam(name = "scenario", required = false) String scenario
    ) {
        JournalDiscountApiReqDTO.Scenario scenarioEnum;
        try {
            scenarioEnum = JournalDiscountApiReqDTO.Scenario.valueOf(scenario);
        } catch (Exception e) {
            throw new IllegalArgumentException(String.format("场景[%s]不存在或错误！", scenario));
        }
        return ResponseEntity.ok(
                this.journalDiscountService.usableList(
                        JournalDiscountApiReqDTO.buildForUsable(
                                studentId,
                                customerId,
                                targetClazzId,
                                LocalDateTimeUtils.parse(bizDateTime),
                                scenarioEnum
                        )
                )
        );
    }

}
